package com.hainan.cs.controller;

import com.hainan.cs.bean.*;
import com.hainan.cs.dao.*;
import com.hainan.cs.singleton.Examine;
import com.hainan.cs.singleton.PassAndName;
import com.hainan.cs.singleton.Questions;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@RequestMapping(value = "/autocreate")
public class AutoCreateController {


    @RequestMapping()
    public ModelAndView auto(String tag) {

        ModelAndView mav = new ModelAndView();
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/application-config.xml");
        PassAndName pan = PassAndName.getInstance();
        String id = null;
        if (pan.getUsername() != null) {
            UserDaoImp udi = context.getBean(UserDaoImp.class);
            UserBean user = udi.queryUser(pan.getUsername()).get(0);
            PaperDaoImp pdi = context.getBean(PaperDaoImp.class);
            PaperBean paper = new PaperBean();
            paper.setPapername("paper" + System.currentTimeMillis() / 1000);
            paper.setType("english");
            paper.setTime("0.5");
            paper.setPaperinfo("this is merged paper");
            paper.setCreatorid(user.getId());
            paper.setLevel(tag);
            id = pdi.insert(paper);
        }

        Random random = new Random();
        HashSet<Integer> set = new HashSet<>();

        ChoiceDaoImp cdi=context.getBean(ChoiceDaoImp.class);
        List<ChoiceQuestionBean> clist=cdi.queryAllPaper(tag);
        if (clist != null && !clist.isEmpty()) {

            //20题
            while (set.size()<clist.size() && set.size()<20){
                Integer i = random.nextInt(clist.size());

                if (set.contains(i)) {
                    continue;
                } else {
                    set.add(i);
                }
                ChoiceQuestionBean choiceQuestionBean = clist.get(i);

                choiceQuestionBean.setId(null);
                choiceQuestionBean.setPaperid(id);
                cdi.insert(choiceQuestionBean);
            }
        }


        MCQDaoImp mdi=context.getBean(MCQDaoImp.class);
        List<MCQBean> mcqBeans = mdi.queryAllPaper(tag);

        set.clear();
        if (mcqBeans != null && !mcqBeans.isEmpty()) {
            //10题
            while (set.size()<mcqBeans.size() && set.size()<10){
                int i = random.nextInt(mcqBeans.size());
                if (set.contains(i)) {
                    continue;
                } else {
                    set.add(i);
                }
                MCQBean mcqBean = mcqBeans.get(i);
                mcqBean.setId(null);
                mcqBean.setPaperid(id);
                mdi.insert(mcqBean);
            }

        }


        JudgeDaoImp jdi=context.getBean(JudgeDaoImp.class);
        List<JudgeQuestionBean> judgeQuestionBeanList = jdi.queryAllPaper(tag);
        set.clear();
        if (judgeQuestionBeanList != null && !judgeQuestionBeanList.isEmpty()) {

            while (set.size()<judgeQuestionBeanList.size() && set.size()<5){
                int i = random.nextInt(judgeQuestionBeanList.size());
                if (set.contains(i)) {
                    continue;
                } else {
                    set.add(i);
                }
                JudgeQuestionBean questionBean = judgeQuestionBeanList.get(i);
                questionBean.setId(null);
                questionBean.setPaperid(id);
                jdi.insert(questionBean);
            }

        }


        QuestionDaoImp qdi=context.getBean(QuestionDaoImp.class);
        List<QuestionBean> list = qdi.queryAll(tag);
        if (list != null&&!list.isEmpty()) {
            int i = random.nextInt(list.size());
            QuestionBean questionBean = list.get(i);
            questionBean.setId(null);
            questionBean.setPaperid(id);
            qdi.insert(questionBean);
        }

        mav.setViewName("redirect:/homepage?tag=" + 1);
        context.close();
        return mav;
    }

}
